/*
 * 描          述:  <描述>
 * 修  改   人:  Administrator
 * 修改时间:  2017年2月10日
 * <修改描述:>
 */
package com.tx.core.remote;

import com.tx.core.exceptions.util.AssertUtils;
import io.swagger.v3.oas.annotations.media.Schema;

import javax.validation.constraints.NotNull;

import org.springframework.http.HttpStatus;

import java.io.Serializable;

/**
 * 远程调用结果<br/>
 * <功能详细描述>
 *
 * @author Administrator
 * @version [版本号, 2017年2月10日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Schema(name = "RemoteResult", description = "调用结果")
public class RemoteResult<T> implements Serializable {

	/** 注释内容 */
	private static final long serialVersionUID = -4513656123138973055L;

	// success_code 成功值 如果是错误时等于错误编码
	public static final int CODE_SUCCESS = 0;

	@Schema(description = "对应HttpStatus.value,与Http请求对应，默认值为HttpStatus.OK.value()即200", example = "200")
	private int status = HttpStatus.OK.value();

	/** 返回码，0-成功，1-通用错误， 其他-详见错误码定义 */
	@Schema(description = "错误编码，0-成功，非0-失败", example = "0")
	@NotNull
	private int code = -1;

	/** 提示消息 */
	@Schema(description = "返回", example = "成功")
	@NotNull
	private String message;

	/** 返回的数据 */
	@Schema(description = "返回数据", example = "成功")
	private T data;

	/** 是否被自动环绕 */
	private boolean autoWrapped = false;

	/**
	 * <默认构造函数>
	 */
	public RemoteResult() {
		super();
	}

	/** <默认构造函数> */
	public RemoteResult(T data) {
		super();

		this.status = HttpStatus.OK.value();
		this.code = RemoteConstants.CODE_SUCCESS;
		this.message = "成功";
		this.data = data;
	}

	/** <默认构造函数> */
	public RemoteResult(int errorCode, String errorMessage) {
		super();

		this.status = HttpStatus.OK.value();
		this.code = errorCode;
		this.message = errorMessage;
		this.data = null;
	}

	/** <默认构造函数> */
	public RemoteResult(int status, int errorCode, String errorMessage) {
		super();

		this.status = status;
		this.code = errorCode;
		this.message = errorMessage;
		this.data = null;
	}

	/** <构造函数> */
	public RemoteResult(int code, String message, T data) {
		super();

		this.status = HttpStatus.OK.value();
		this.code = code;
		this.message = message;
		this.data = data;
	}

	/**
	 * 是否成功<br/>
	 * <功能详细描述>
	 * 
	 * @return [参数说明]
	 * 
	 * @return boolean [返回类型说明]
	 * @exception throws [异常类型] [异常说明]
	 * @see [类、类#方法、类#成员]
	 */
	public boolean isError() {
		if (HttpStatus.OK.value() != this.status) {
			return true;
		}
		return false;
	}

	/**
	 * 是否成功<br/>
	 * <功能详细描述>
	 * 
	 * @return [参数说明]
	 * 
	 * @return boolean [返回类型说明]
	 * @exception throws [异常类型] [异常说明]
	 * @see [类、类#方法、类#成员]
	 */
	public boolean isSuccess() {
		if (CODE_SUCCESS == this.code && HttpStatus.OK.value() == this.status) {
			return true;
		}
		return false;
	}

	/**
	 * @return 返回 code
	 */
	public int getCode() {
		return code;
	}

	/**
	 * @param code
	 */
	public void setCode(int code) {
		this.code = code;
	}

	/**
	 * @return 返回 message
	 */
	public String getMessage() {
		return message;
	}

	/**
	 * @param message
	 */
	public void setMessage(String message) {
		this.message = message;
	}

	/**
	 * @return 返回 data
	 */
	public T getData() {
		return data;
	}

	/**
	 * @param data
	 */
	public void setData(T data) {
		this.data = data;
	}

	/**
	 * @return the status
	 */
	public int getStatus() {
		return status;
	}

	/**
	 * @param status the status to set
	 */
	public void setStatus(int status) {
		this.status = status;
	}

	/**
	 * @return the autoWrapped
	 */
	public boolean isAutoWrapped() {
		return autoWrapped;
	}

	/**
	 * @param autoWrapped the autoWrapped to set
	 */
	public void setAutoWrapped(boolean autoWrapped) {
		this.autoWrapped = autoWrapped;
	}

	/**
	 * 构建成功对象<br/>
	 * <功能详细描述>
	 * 
	 * @param data
	 * @return [参数说明]
	 * 
	 * @return Result<OBJECT> [返回类型说明]
	 * @exception throws [异常类型] [异常说明]
	 * @see [类、类#方法、类#成员]
	 */
	public static RemoteResult<Boolean> SUCCESS(Boolean bool) {
		RemoteResult<Boolean> remoteResult = new RemoteResult<Boolean>(bool);
		return remoteResult;
	}

	public static <OBJECT> RemoteResult<OBJECT> SUCCESS(OBJECT data) {
		AssertUtils.notNull(data, "data is null.");

		RemoteResult<OBJECT> remoteResult = new RemoteResult<OBJECT>(data);
		return remoteResult;
	}

	public static RemoteResult<?> FAIL(int errorCode, String errorMessage) {
		RemoteResult<?> remoteResult = new RemoteResult<Object>(errorCode, errorMessage, null);
		return remoteResult;
	}

	public static <OBJECT> RemoteResult<OBJECT> RESULT(int errorCode, String errorMessage, OBJECT data) {
		RemoteResult<OBJECT> remoteResult = new RemoteResult<OBJECT>(errorCode, errorMessage, data);
		return remoteResult;
	}

	public static RemoteResult<?> ERROR(int httpStatus, int errorCode, String errorMessage) {
		RemoteResult<?> remoteResult = new RemoteResult<Object>(httpStatus, errorCode, errorMessage);
		return remoteResult;
	}

	public static RemoteResult<?> ERROR(int httpStatus, String errorMessage) {
		RemoteResult<?> remoteResult = new RemoteResult<Object>(httpStatus, httpStatus, errorMessage);
		return remoteResult;
	}

}
